/*
 * 创建日期 2005-5-26
 *
 * 更改所生成文件模板为
 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释
 */
package com.guanghua.brick.db;

import java.util.Map;

import com.guanghua.brick.html.Constant;



/**
 * @author leeon
 *
 * 更改所生成类型注释的模板为
 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释
 */
public class OrderInfo {
	
	private String defaul = null;
	private String orderKey = null;
	private String[] orderProperties = null;
	private String[] objectProperties = null;
	
	private String order = null;
	public String getOrder() {return (order!=null&&order.length()!=0)?" order by " + order:"";}
	
	private OrderInfo() {}
	
	private OrderInfo(Map req, String defaul, String prefix, String[] orderProperties, String[] objectProperties) {
		this.defaul = defaul;
		this.orderKey = prefix + "." + Constant.GLOBE_ORDER;
		this.orderProperties = orderProperties;
		this.objectProperties = objectProperties;
		
		doOrder(req);
	}	
	
	private void doOrder(Map req) {
		order = defaul;
		if (orderKey == null || !req.containsKey(orderKey)) return;
		if (req.get(orderKey) == null) return;
		if (toString(req.get(orderKey)).length() == 0) return;
					
		order = toString(req.get(orderKey));
			
		if (orderProperties != null) {
				
			StringBuffer sb = new StringBuffer();
			String[] keys = order.split(",");
				
			//根据逗号分隔来判断每一个排序字段
			for (int i = 0;i < keys.length; i++) {
				keys[i] = keys[i].trim();
				//找到空格
				int j = keys[i].indexOf(" ");
				String t = null;
				
				//有空格,说明有desc 或者 asc字符串
				if (j != -1)
					t = keys[i].substring(0, j);
				else
					t = keys[i];
				
				//再order property中寻找替换的值
				for (int k = 0; k < orderProperties.length; k++) {
					if (orderProperties[k].equals(t)) {
						//为被替换成的新字符串
						t = objectProperties[k];
						break; 	
					}
				}
				
				//有空格,有desc或者asc的情况
				if (j != -1) {
					String[] ts = t.split(",");
					for (int k = 0;k < ts.length; k++) {					
						sb.append(ts[k].trim())
						.append(keys[i].substring(j))
						.append(", ");
					}
				//没有空格,直接替换成新字符串
				} else {
					sb.append(t)
					.append(", ");
				}
			}
			
			order = sb.substring(0, sb.length() - 2);			
		}
	}
	
	public static OrderInfo newInstance(Map req, String defaul, String prefix, String[] orderProperties, String[] objectProperties) {
		return new OrderInfo(req, defaul, prefix, orderProperties, objectProperties);
	}
	
	public static OrderInfo newInstance(Map req, String defaul, String[] orderProperties, String[] objectProperties) {
		return new OrderInfo(req, defaul, Constant.GLOBE, orderProperties, objectProperties);
	}
	
	public static OrderInfo newInstance(Map req, String defaul, String prefix) {
		return newInstance(req, defaul, prefix, null, null);
	}	
	
	public static OrderInfo newInstance(Map req, String defaul) {
		return newInstance(req, defaul, Constant.GLOBE);		
	}
	
	/**
	 * 主要用于将单个的String数组转为String
	 * 取String数组的第一个值
	 * @param o
	 * @return
	 */
	private static String toString(Object o) {
		if (o instanceof String[]) {
			return ((String[])o)[0];
		} else if (o instanceof String) {
			return (String)o;
		} else {
			return o.toString();
		}
	}
	
	public static void main(String[] args) throws Exception {
		Map<String, String> map = new java.util.HashMap<String, String>();
		map.put("globe.order", "a1, b1, c1 desc, d1 asc");
		String d = "a desc, b, c asc, d";
		String[] orderp = {"a1", "d1"};
		String[] objectp = {"aa1.a, aa1.f", "dd1.d"};
		System.out.println(OrderInfo.newInstance(map, d, orderp, objectp).getOrder());
		
	}

}
